package cn.xerllent.common.utils.jsonserializer;

import java.io.IOException;
/**
 * 自定义事件格式化成ofEpochSecond
 * */
import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneOffset;

import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.JsonDeserializer;

import cn.hutool.core.util.ObjectUtil;

public class LocalDateTimeDeserializer extends JsonDeserializer<LocalDateTime> {

    /*
     * @param p
     * @param ctxt
     * @return
     * @throws IOException
     * @throws JsonProcessingException (non-Javadoc)
     * @see com.fasterxml.jackson.databind.JsonDeserializer#deserialize(com.fasterxml.jackson.core.JsonParser,
     * com.fasterxml.jackson.databind.DeserializationContext)
     */
    @Override
    public LocalDateTime deserialize(JsonParser p, DeserializationContext ctxt) throws IOException {
        String date = p.getText();
        if (ObjectUtil.isEmpty(date)) {
            return null;
        } else {
            return LocalDateTime.ofInstant(Instant.ofEpochSecond(Long.parseLong(date)), ZoneOffset.of("+8"));
        }
    }

}